package com.gitee.wsl.css.selector.builder

import com.gitee.wsl.css.components.PageInterface
import com.gitee.wsl.css.property.Keyframes
import com.gitee.wsl.css.rule.RuleSet
import com.gitee.wsl.css.selector.ClassSelector
import com.gitee.wsl.css.selector.Selector
import com.gitee.wsl.css.selector.StringSelector

fun PageInterface.classRuleSet(classNamePrefix: String? = null, rules: RuleSet.() -> Unit): RuleSet {
    val className = classNameGenerator.getClassName(classNamePrefix)
    val selector = ClassSelector(className)
    val ruleSet = RuleSet(this, selector).apply { rules() }
    addRuleSetToDocument(ruleSet)
    return ruleSet
}

fun PageInterface.classRuleSet(classNamePrefix: String? = null, ruleSet: RuleSet): RuleSet {
    val className = classNameGenerator.getClassName(classNamePrefix)
    val selector = ClassSelector(className)
    ruleSet.selector = selector
    addRuleSetToDocument(ruleSet)
    return ruleSet
}

fun PageInterface.stringRuleSet(selector: String, rules: RuleSet.() -> Unit): RuleSet {
    val stringSelector = StringSelector(selector)
    val ruleSet = RuleSet(this, stringSelector).apply { rules() }
    addRuleSetToDocument(ruleSet)
    return ruleSet
}

fun PageInterface.ruleSet(selector: Selector, rules: RuleSet.() -> Unit): RuleSet {
    val ruleSet = RuleSet(this, selector).apply { rules() }
    addRuleSetToDocument(ruleSet)
    return ruleSet
}

fun PageInterface.keyframes(userIndent: String, keyframesRules: Keyframes.() -> Unit): Keyframes {
    val keyframes = Keyframes(this, userIndent).apply { keyframesRules() }
    addKeyframesToDocument(keyframes)
    return keyframes
}

fun Selector.ruleSet(pageInterface: PageInterface,rules: RuleSet.() -> Unit) = pageInterface.ruleSet(this,rules)